<?php

namespace App\Http\Controllers\user;

use App\Dao\BillsScoreDao;
use App\Dao\CategoryDao;
use App\Dao\ChatsDao;
use App\Dao\CommonDao;
use App\Dao\CursorsDao;
use App\Dao\GoodsDao;
use App\Dao\OrdersDao;
use App\Dao\SettingsDao;
use App\Dao\UsersDao;
use App\Services\Code;
use App\Services\FadadaServices;
use App\Services\OssServices;
use FddCloud\constants\OpenApiConfig;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class CommonController extends BaseController
{
    public function categorys(): array
    {
        return Code::message('',200,CategoryDao::get_datas());
    }

    public function commons(): array
    {
        return Code::message('',200,[
            'now_time_price'=>CommonDao::real_time_price(),
            'sys_balance'=>SettingsDao::get_set_value('sys_balance'),
            'sys_score_all'=>SettingsDao::get_set_value('sys_score_all'),
            'img_url_pay'=>SettingsDao::get_set_value('img_url_pay'),
        ]);
    }

    public function test()
    {
        //new OpenApiConfig();
    //FadadaServices::getAccessToken();exit();
//    FadadaServices::fadada_user_auth([]);
//        FadadaServices::create_with_template();exit();
//        $resyl = FadadaServices::get_template_info();
//        var_dump($resyl);
//        FadadaServices::fill_template_content($resyl['data_re']);

        //FadadaServices::get_sign_url();
        return QrCode::size(200)->generate('http://www.baidu.com');
    exit();
//        $res = CommonDao::real_time_price();
//        var_dump($res);

//        CommonDao::ShengYuScoreJingTai();
//        $order_lists = OrdersDao::builder_query()->get()->toArray();
//        foreach ($order_lists as $value){
//            OrdersDao::builder_query([
//                'order_id'=>$value['order_id']
//            ])->update([
//                'price'=>json_decode($value['order_json_data'],true)['goods_price']
//            ]);
//        }
//        return;
//        CategoryDao::get_category_builder()->delete();
        return;

        $l_arr = [0,3,4,5,6,7,8];
        $yeJi = [0,26000,78000,260000,1300000,5200000,15600000];

        //查询所有用户
        $users = UsersDao::builder_user_query()->orderByDesc('created_at')->get()->toArray();
        foreach ($users as $value){
            $spread_id = $value['spread_id'];
            while(true){
                if($spread_id == 0){
                    break;
                }
                //查询上级用户的下级数量
                $cCount = UsersDao::builder_user_query([
                    'spread_id'=>$spread_id
                ])->where('vip_index_level',1)->count();

                $u_spreadInfo = UsersDao::builder_user_query([
                    'user_id'=>$spread_id
                ])->first()->toArray();
                if($u_spreadInfo['vip_level'] == 6){
                    break;
                }
                if($cCount < $l_arr[$u_spreadInfo['vip_level']+1]){

                    break;
                }

                //查询业绩
                //业绩活口
                $yeJiMaxBe = $yeJi[$u_spreadInfo['vip_level']+1];
                if($u_spreadInfo['all_prices'] < $yeJiMaxBe){
                    break;
                }
                //获取直推的最大业绩
                $max_all_prices = OrdersDao::getMaxPrices($spread_id);
                //小区业绩只和必须大于大区1/2自己的业绩减去下级最大业绩
                $Ji = $u_spreadInfo['all_prices'];
                $MaxPrij = $max_all_prices;
                $xiaoJi = bcsub($Ji,$MaxPrij,2);
                if($xiaoJi < bcmul($MaxPrij,0.5,2)){
                    break;
                }



                UsersDao::builder_user_query([
                    'user_id'=>$spread_id
                ])->update([
                    'vip_level'=>$u_spreadInfo['vip_level']+1
                ]);
                $spread_id = UsersDao::builder_user_query([
                    'user_id'=>$spread_id
                ])->value('spread_id');
            }
        }


    }


    public function qr_code()
    {
        $params = request()->all();
        return Code::message('',200,CommonDao::qr_code($params));
    }

    public function cursors(): array
    {
        return Code::message('',200,CursorsDao::cursorsWithNoPage([]));
    }


    public function send_sms(): array
    {
        $params = request()->all();
        CommonDao::send_sms($params);
        return Code::message('',200);
    }


    public function file_up(): array
    {
        ini_set('memory_limit','2048M');
        set_time_limit(0);
        $filePath = $_FILES["file"]["tmp_name"];
        $ext = substr($_FILES['file']['name'],strrpos($_FILES['file']['name'],'.')+1); // 上传文件后缀
        $accept = request('accept');
//        if(!in_array(strtolower($ext),$this->arr_ext[$accept])){
//            return Code::message('文件格式不正确!!',301);
//        }
        $object=date('YmdHis').'-'.time().'.'.$ext;
        $oss = new OssServices();
        $url = $oss->upload($object,$filePath);
        return Code::message('',200,[
            'src'=>$url
        ]);

    }


    public function plutocrats(): array
    {
        return Code::message('',200,BillsScoreDao::plutocrats());
    }

    public function add_chat(): array
    {
        $params = array_merge([
            'user_id'=>getUserToken()
        ],request()->all());
        ChatsDao::add_chats($params);
        return Code::message('',200);
    }
    public function chats(): array
    {
        $params = array_merge([
        ],request()->all());
        return Code::message('',200,ChatsDao::chats($params));
    }
}
